package com.hs.controller;

import com.github.pagehelper.PageInfo;
import com.hs.entity.Question;
import com.hs.service.QuestionService;
import com.hs.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("Question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @PostMapping
    public R addQuestion(@RequestBody Question question) {
        int i = questionService.addQuestion(question);
        if(i>0){
            return R.ok();
        }else{
            return R.fail("增加失败");
        }
    }
    @DeleteMapping("{id}")
    public R deleteQuestion(@PathVariable int id) {
        int i = questionService.deleteQuestion(id);
        if(i>0){
            return R.ok();
        }else{
            return R.fail("删除失败");
        }
    }
    @PutMapping
    public R updateQuestion(@RequestBody Question question) {
        int i = questionService.updateQuestion(question);
        if(i>0){
            return R.ok();
        }else{
            return R.fail("修改失败");
        }
    }
    @GetMapping("{id}")
    public R getQuestionById(@PathVariable int id) {
        Question question = questionService.getQuestionById(id);
        return R.ok(question);
    }
    @GetMapping("getQuestionByPage")
    public R getQuestionByPage(@RequestParam(required = false,defaultValue = "1")int pageNum, @RequestParam(required = false,defaultValue = "5")int pageSize) {
        PageInfo<Question> questionByPage = questionService.getQuestionByPage(pageNum, pageSize);
        return R.ok(questionByPage);
    }
}
